package com.qingxin.sys.verifier;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;

import javax.servlet.http.HttpServletRequest;

/**
 * 保存表单数据的MAP对象
 * <br>
 * @author JiWangli
 */
public class FormMap extends HashMap<String, String[]> {
	private static final long serialVersionUID = -9213253018154915818L;
	
	private HttpServletRequest request;
	
	public FormMap() {
	}
	
	public FormMap(HttpServletRequest request) {
		this.request = request;
	}
	
	
	public HttpServletRequest getRequest() {
		return request;
	}
	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}
	
	
	/**
	 * 添加一条数据
	 * 
	 * @param key
	 * @param value
	 */
	public void putEntry(String key, String value){
		put(key, new String[]{value});
	}
	
	
	/**
	 * 获取参数值
	 * 
	 * @param name
	 * @return
	 */
	public String getParameter(String name) {
		if(request != null){
			String value = request.getParameter(name);
			if(value != null){
				return value;
			}
		}
		String[] values = get(name);
		if(values != null && values.length > 0){
			return values[0];
		}
		return null;
	}
	
	
	/**
	 * 获取参数值数组
	 * 
	 * @param name
	 * @return
	 */
	public String[] getParameterValues(String name) {
		if(request != null){
			String[] values = request.getParameterValues(name);
			if(values != null){
				return values;
			}
		}
		return get(name);
	}
	
	
	public Map<String, String[]> getParameterMap() {
		Map<String, String[]> map = new HashMap<String, String[]>();
		if(request != null && request.getParameterMap() != null){
			map.putAll(request.getParameterMap());
		}
		map.putAll(this);
		return map;
	}
	
	
	public Enumeration<String> getParameterNames() {
		Vector<String> names = new Vector<String>();
		if(request != null && request.getParameterMap() != null){
			names.addAll(request.getParameterMap().keySet());
		}
		names.addAll(keySet());
		return names.elements();
	}
	
	
	
}
